<script src="/public/js/jquery.ui.widget.js"></script>
<script src="/public/js/jquery.iframe-transport.js"></script>
<script src="/public/js/jquery.fileupload.js"></script>
<script type="text/javascript">
jQuery(function ($) {
    'use strict';
    // Change this to the location of your server-side upload handler:
    var url = '<?=site_url('member/upload')?>';
    $('#selectImageBtn').fileupload({
        url: url,
        dataType: 'json',
        done: function (e, data) {
            $('.selectbtn').css('backgroundPosition','-100px top');
            if(data.result.errorcode != 0){
                return tip(data.result.error,data.result.errorcode,2);
            }
            insertImage(document.workpub.imageall,data.result.files, data.result.baseurl);
            
        },
        progressall: function (e, data) {
            var progress = parseInt(data.loaded / data.total * 100, 10)-100;
            $('.selectbtn').css('backgroundPosition',progress+'px top');
        }
    }).prop('disabled', !$.support.fileInput)
        .parent().addClass($.support.fileInput ? undefined : 'disabled');
});
jQuery(function($){
    $(document.workpub).validation();
})
</script>
<form name="workpub" method="post" class="workpublish">
<div class="clearfix textline">
<label>主题：</label>
<input type="text" class="text" name="title" required="required" requiretip="请填写作品主题" />
</div>
<div class="clearfix textline">
<label>图片：</label><div class="imageviewbox"><input type="hidden" name="imagedefault" id="imagedefault" />
<input type="hidden" name="imageall" id="imageall" /><a href="javascript:;" class="selectbtn">选择<input type="file" names="files" id="selectImageBtn" multiple /></a>
</div>
</div>
<div class="clearfix editline">
<label>故事：</label><label><input type="checkbox" name="iscomm" value="1" />&nbsp;推荐作品</label><span style="vertical-align:middle; line-height:34px; color:#ccc"> （最多不要超过600字）</span>
<div class="clear"></div>
<textarea name="content" class="editor"  id="txta1"></textarea>
</div>
<div class="submitline">
    <input type="submit" class="ani button2" name="submit" value="发表故事" />
</div>
</form>
<script type="text/javascript" >
function insertImage(field,files,baseurl){
    var images=[];
    if(field.value){
        images=field.value.split(',');
    }
    for(var i=0;i<files.length;i++){
        files[i].url=baseurl+files[i].file_name;
        if(!in_array(images,files[i].url)){
            images.push(files[i].url);
            renderfile(files[i],field);
        }else{
            tip('图片 '+ files[i].file_name +' 已添加过了',1,2);
        }
    }
    field.value=images.join(',');
}
function renderfile(file,field){
    $(field).before('<div class="imageview"><input type="hidden" name="image[]" value="'+file.url+'" /><img src="'+file.url+'" alt=""/><a href="javascript:" class="ibtn defaultibtn" onclick="setDefault(this)">更换</a><a href="javascript:" class="ibtn" onclick="removeImg(this)">移除</a></div>');
    if(document.workpub.imagedefault.value==''){
        document.workpub.imagedefault.value=file.url;
        $('.imageviewbox .imageview').eq(0).addClass('default');
    }
}
function in_array(arr,value){
    for(var i=0;i<arr.length;i++){
        if(value==arr[i]){
            return true;
        }
    }
    return false;
}
function setDefault(obj){
    var view=$(obj).parents('.imageview').eq(0);
    $('.imageviewbox .imageview').removeClass('default');
    view.addClass('default');
}
function removeImg(obj){
    var view=$(obj).parents('.imageview').eq(0);
    if(confirm('您确定移除此图片？')){
        var isdefault=false;
        if(view.is('.default')){
            isdefault=true;
        }
        var file=view.find('input').val();
        $.ajax({
            'url':'<?=site_url('member/upload_delete')?>',
            'data':'file='+file,
            'type':'post'
        })
        view.remove();
        setDefault($('.imageviewbox .defaultibtn').eq(0));
    }
}


//字数
window.onload = function() 
{ 
	document.getElementById('txta1').onkeyup = function() 
	{    
		if(this.value.length >= 600) {	
			var num=this.value.substr(0,600);
			alert('你的故事已经超过600字！');
		}
		 
	} 
} 
</script>